package cn.zyb.org.controller;

import cn.zyb.basic.exception.BusinessException;
import cn.zyb.basic.util.AjaxResult;
import cn.zyb.basic.util.ExcelUtils;
import cn.zyb.basic.util.PageList;
import cn.zyb.org.domain.Shop;
import cn.zyb.org.domain.ShopAuditLog;
import cn.zyb.org.domain.ShopDto;
import cn.zyb.org.query.ShopQuery;
import cn.zyb.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 接口类：店铺
 */
@RestController
@RequestMapping("/shop")
@Api(value = "店铺接口类",description = "店铺接口的CRUD操作")
public class ShopController {
    @Autowired
    private IShopService shopService;

    /**
     * 接口：查询所有
     * @return
     */
    // http://localhost:8080/shop - Get
    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<Shop> findAll(){
        return shopService.findAll();
    }

    /**
     * 接口：查询单个对象
     * @return
     */
    // http://localhost:8080/shop/2 - Get
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个对象")
    public Shop findOne(@PathVariable("id") Long id){
        return shopService.findOne(id);
    }

    /**
     * 分页接口
     * @param shopQuery
     * @return
     */
    // http://localhost:8080/shop - Post - 参数通过json对象传递
    @PostMapping
    //@RequestBody -从请求体中获取数据赋值给shopQuery
    @ApiOperation(value = "分页接口")
    public PageList<Shop> queryPage(@RequestBody ShopQuery shopQuery){
        return shopService.queryPage(shopQuery);
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    // http://localhost:8080/shop/2 - Delete
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            shopService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
//            return AjaxResult.me().setSuccess(false).setMsg("删除失败");
        }
    }

    /**
     * 接口：添加和修改
      * @param shop
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改接口")
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if(shop.getId()==null){//添加
                shopService.add(shop);
            }else{
                shopService.update(shop);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

  /**
   * 接口：批量删除
   * @param ids
   * @return
   */
  @PatchMapping
  @ApiOperation(value = "批量删除接口")
  public AjaxResult patchDel(@RequestBody List<Long> ids){
    try {
      shopService.patchDel(ids);
      return new AjaxResult();
    } catch (Exception e) {
      e.printStackTrace();
      return new AjaxResult(false,"删除失败");
    }
  }

  /**
   * 接口：店铺入驻
   * @param shop
   * @return AjaxResult 响应给前端
   */
  @PostMapping("/settlement")
  public AjaxResult settlement(@RequestBody Shop shop){
    try {
      shopService.settlement(shop);
      return new AjaxResult();
    } catch (BusinessException e) {
      e.printStackTrace();
      return new AjaxResult(false,e.getMessage());
    } catch (Exception e) {
      e.printStackTrace();
      return new AjaxResult(false,"系统异常，请稍后重试!!!");
    }
  }
  /**
   * 接口：店铺审核成功
   * @param shopAuditLog
   * @return AjaxResult 响应给前端
   */
  @PostMapping("/audit/pass")
  public AjaxResult auditPass(@RequestBody ShopAuditLog shopAuditLog){
    try {
      shopService.auditPass(shopAuditLog);
      return new AjaxResult();
    } catch (Exception e) {
      e.printStackTrace();
      return new AjaxResult(false,"系统异常，请稍后重试!!!");
    }
  }


  /**
   * 接口：店铺审核失败
   * @param shopAuditLog
   * @return AjaxResult 响应给前端
   */
  @PostMapping("/audit/reject")
  public AjaxResult auditReject(@RequestBody ShopAuditLog shopAuditLog){
    try {
      shopService.auditReject(shopAuditLog);
      return new AjaxResult();
    } catch (Exception e) {
      e.printStackTrace();
      return new AjaxResult(false,"系统异常，请稍后重试!!!");
    }
  }

  @GetMapping("/export")
  public void export( HttpServletResponse response){
    try {
      List<Shop> shops = shopService.findAll();
      ExcelUtils.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xlsx", response);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  @PostMapping("/importExcel")
  public void importExcel(@RequestPart("file") MultipartFile file){
    //String file = "F:\\文件名.xls";
    //解析excel，
    List<Shop> list = ExcelUtils.importExcel(file,0,1,Shop.class);
    //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入
    System.out.println("导入数据一共【"+list.size()+"】行");
    for (Shop shop:list) {
      System.out.println(shop);
    }
    //保存到数据库中[自己实现，提示：动态sql 批量插入]
  }

  @GetMapping("/echarts")
  public List<ShopDto> echartsData(){
    List<ShopDto> shopDtos = shopService.getCountByState();
    return shopDtos;
  }

  @GetMapping("/active/{id}")
  @ApiOperation(value = "查询单个对象")
  public AjaxResult active(@PathVariable("id") Long id){
    try {
      shopService.active(id);
      return new AjaxResult();
    } catch (Exception e) {
      e.printStackTrace();
      return new AjaxResult(false,"激活失败");
    }
  }


}
